//
//  USChatImageCell.m
//  us
//
//  Created by karry on 2025/4/6.
//

#import "USChatImageCell.h"

@implementation USChatImageCell

- (void)setupView{
    [super setupView];
 
    self.contentImageView = [[UIImageView alloc]initWithImage:nil];
    self.contentImageView.layer.cornerRadius = 14;
    self.contentImageView.layer.masksToBounds = YES;
//    [UIImage jk_imageWithColor:[UIColor.jk_randomMacaronColor colorWithAlphaComponent:.3]]
    self.contentImageView.backgroundColor = [UIColor.jk_randomMacaronColor colorWithAlphaComponent:.3];
    [self.messageContentView addSubview:self.contentImageView];
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_offset(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
}

- (void)setMessageViewModel:(USMessageViewModel *)messageViewModel{
    [super setMessageViewModel:messageViewModel];
    
    EMChatMessage *message = messageViewModel.message;
    EMImageMessageBody *body = (EMImageMessageBody *)message.body;
    
    if (message.direction == EMMessageDirectionSend) {
        if (body.localPath.length > 0) {
            
            [self.contentImageView sd_setImageWithURL:[NSURL fileURLWithPath:body.localPath] placeholderImage:nil];
            
        }else if (body.thumbnailRemotePath.length > 0){
            
            [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:body.thumbnailRemotePath] placeholderImage:nil];
        }else {
            [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:body.remotePath] placeholderImage:nil];

        }
    }else {
        [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:body.remotePath] placeholderImage:nil];

    }

    
}

- (void)updateLayout{

    [super updateLayout];
    
    EMChatMessage *message = self.messageViewModel.message;
    
    EMImageMessageBody *body = (EMImageMessageBody *)message.body;
    
    CGSize size = body.size;
    
    size = [self scaledSizeForSize:size];
    
//    NSLog(@"thumbnailSize - %@",[NSValue valueWithCGSize:size]);

    if (self.messageViewModel.message.direction == EMMessageDirectionSend) {
        
        [self.contentImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.left.top.bottom.mas_offset(0);
            make.width.mas_equalTo(size.width);
            make.height.mas_equalTo(size.height);
        }];
        
    }else{
        
        [self.contentImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.left.top.bottom.mas_offset(0);
            make.width.mas_equalTo(size.width);
            make.height.mas_equalTo(size.height);
        }];
        
    }
    
    
}

- (CGSize)scaledSizeForSize:(CGSize)size{
    CGSize originalSize = size;
    CGFloat maxSide = 200;
    CGFloat minSide = 40;

    // 保持宽高比，计算缩放比例
    CGFloat scale = MIN(maxSide / MAX(originalSize.width, originalSize.height), 1.0);
    CGFloat width = originalSize.width * scale;
    CGFloat height = originalSize.height * scale;

    // 再确保不会小于最小值
    width = MAX(width, minSide);
    height = MAX(height, minSide);
    
    return CGSizeMake(ceilf(width), ceilf(height));
    
}


@end
